package MapBase.logic.control.xml
{
	import MapBase.logic.untils.MapConfig;
	
	import com.friendsofed.isometric.IsoObject;
	import com.friendsofed.isometric.IsoWorld;
	import com.utils.astar.Grid;

	public class MapXML
	{
		public var grid : Grid;
		public var xml : XML;
		
		public var numCols : int;
		public var numRows : int;
		
		public var tileSize : Number;
		
		public var mapName : String = "mapData";
		public var bkMapName : String
		
		public var mapType : int = 1;
		
		public var world : IsoWorld;
		
		public function MapXML(grid:Grid=null,isoWorld:IsoWorld=null,mapType:int=1)
		{
			this.grid = grid;
			this.world = isoWorld;
			this.mapType = mapType;
		}
		
		public function save():void
		{
			create();
			
			var file:MapConfig  = new MapConfig;
			file.saveConfig(xml,mapName);
		}
		
		public function create():void
		{
			var floorData : Array = [];
			
			if(!grid) return;
			
			numCols = grid.numCols;
			numRows = grid.numRows;
			
			for(var i:int=0;i<grid.numCols;i++)
			{
				for(var j:int=0;j<grid.numRows;j++)
				{
					if(grid.getNode(i,j).walkable)
					{
						floorData.push(1);
					}
					else
					{
						floorData.push(0);
					}
				}
			}
			
			xml = <map>
							<name>default</name>
							<type>1</type>
							<bkMapName>default</bkMapName>
							<tileSize></tileSize>
							<colNum></colNum>
							<rowNum></rowNum>
							<isoBox></isoBox>
							<floor></floor>
					</map>;
			xml["name"] = mapName;
			xml["type"] = mapType;
			xml["bkMapName"] = bkMapName;
			xml["colNum"] = numCols;
			xml["rowNum"] = numRows;	
			xml["tileSize"] = tileSize*2;
			
			for each(var isoObj:IsoObject in world.objects)
			{
				var appendXml : XML = <item/>;
				appendXml.@id = isoObj.id;
				appendXml.@isoX = isoObj.x;
				appendXml.@isoZ = isoObj.z;
				xml["isoBox"].appendChild(appendXml);
			}
			
			xml["floor"] = floorData.toString();
		}
	}
}